package dk.eclipse.android.badassapp.sensor.accelerometer;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;
import dk.eclipse.android.badassapp.R;

/**
 * This activity explores the sensor API and services.
 * 
 * @author Ronni Kahalani (eclipse.dk)
 */
public class AccelerometerActivity extends Activity implements
		AccelerometerListener {
	private static final String NOT_AVAILABLE = "N/A";

	/**
	 * Tag used for logging information.
	 */
	private static final String LOG_TAG = AccelerometerActivity.class
			.getSimpleName();

	private static Context CONTEXT;

	private TextView accelerometerXTextView;
	private TextView accelerometerYTextView;
	private TextView accelerometerZTextView;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_LEFT_ICON);
		setContentView(R.layout.sensor_accelerometer);
		setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_app);

		accelerometerXTextView = (TextView) findViewById(R.id.accelerometerX);
		accelerometerYTextView = (TextView) findViewById(R.id.accelerometerY);
		accelerometerZTextView = (TextView) findViewById(R.id.accelerometerZ);

		CONTEXT = this;

		String initValue = "0";
		if (!AccelerometerManager.isSupported()) {
			initValue = NOT_AVAILABLE;
		}
		accelerometerXTextView.setText(initValue);
		accelerometerYTextView.setText(initValue);
		accelerometerZTextView.setText(initValue);

	}

	protected void onResume() {
		super.onResume();
		if (AccelerometerManager.isSupported()) {
			AccelerometerManager.startListening(this);
		}
	}

	protected void onDestroy() {
		super.onDestroy();
		if (AccelerometerManager.isListening()) {
			AccelerometerManager.stopListening();
		}
	}

	public static Context getContext() {
		return CONTEXT;
	}

	/**
	 * onShake callback
	 */
	public void onShake(float force) {
		Toast.makeText(this, "Phone shaked : " + force, Toast.LENGTH_LONG)
				.show();
	}

	/**
	 * onAccelerationChanged callback
	 */
	public void onAccelerationChanged(float x, float y, float z) {
		accelerometerXTextView.setText(String.valueOf(x));
		accelerometerYTextView.setText(String.valueOf(y));
		accelerometerZTextView.setText(String.valueOf(z));
	}

}